package com.atguigu.springboot.utils;

import com.atguigu.springboot.bean.DeptTreeVo;
import com.atguigu.springboot.bean.SysDept;
import com.atguigu.springboot.bean.SysMenu;
import lombok.experimental.UtilityClass;

import java.util.ArrayList;
import java.util.List;

@UtilityClass
public class PreUtil {


    public void findChildren1(List<DeptTreeVo> sysDepts, List<SysDept> depts){
        for (DeptTreeVo sysDept : sysDepts) {
            sysDept.setId(sysDept.getId());
            sysDept.setLabel(sysDept.getLabel());
            List<DeptTreeVo> children = new ArrayList<>();
            for (SysDept dept : depts) {
                if (sysDept.getId() == dept.getParentId()){
                    DeptTreeVo deptTreeVo1 = new DeptTreeVo();
                    deptTreeVo1.setId(dept.getDeptId());
                    deptTreeVo1.setLabel(dept.getName());
                    children.add(deptTreeVo1);
                }
            }
            sysDept.setChildren(children);
            findChildren1(children,depts);
        }
    }


    /**
     * 构建部门菜单不采用Vo对象的方式
     *此种方式的思路为:
     * 1、把所有的根节点先拿到,遍历根节点，把子节点数据添加至根节点
     * 2、通过递归的方式把下级节点也加到，更下级节点中
     * @param rootMenus
     * @param sysMenus
     */
    public void getChildMenu(List<SysMenu> rootMenus,List<SysMenu> sysMenus){
        for (SysMenu rootMenu : rootMenus) {
            List<SysMenu> childMenus = new ArrayList <SysMenu>();
            for (SysMenu sysMenu : sysMenus) {
                if (sysMenu.getParentId().equals(rootMenu.getMenuId())){
                    childMenus.add(sysMenu);
                }
            }
            rootMenu.setChildren(childMenus);
            getChildMenu(childMenus,sysMenus);
        }
    }
}
